import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:speed_random/widgets/item_history_list.dart';
import 'package:speed_random/widgets/not_data.dart';

import 'logic.dart';
import 'state.dart';

class NumResultsPage extends StatelessWidget {
  NumResultsPage({Key? key}) : super(key: key);

  final NumResultsLogic logic = Get.put(NumResultsLogic());
  final NumResultsState state = Get.find<NumResultsLogic>().state;

  // 显示确认对话框
  Future<void> _showClearConfirmationDialog(BuildContext context) async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false, // 用户不能通过点击对话框外部关闭对话框
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('确认清空'),
          content: const SingleChildScrollView(
            child: ListBody(
              children: <Widget>[
                Text('确定要清空所有随机数历史记录吗？'),
              ],
            ),
          ),
          actions: <Widget>[
            TextButton(
              child: const Text('取消'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: const Text('确定'),
              onPressed: () {
                logic.clearHistory();
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:  Obx(()=>Text('历史记录'.tr+'(${state.historyNumbers.value.length})')),
        //backgroundColor: AppStyles.colorAppBarbg,
        actions: [
          IconButton(
            icon: const Icon(Icons.delete),
            onPressed: () {
              _showClearConfirmationDialog(context);
            },
          ),
        ],
      ),
      body: Obx(() =>state.historyNumbers.value.length==0? notData(): ListView.builder(
        itemCount: state.historyNumbers.value.length,
        itemBuilder: (context, index) {
          final indext = state.historyNumbers.value.length - 1 - index;
          final numbers = state.historyNumbers.value[indext];
          if(index==2 && state.bannerAdWidget!=null){
            return Column(children: [

              state.bannerAdWidget!,
              itemHistoryCard(numbers: numbers)
            ],);
          }

          return itemHistoryCard(numbers: numbers);
        },
      )),
    );
  }
}
